package com.beordie.dao.impl;

import com.beordie.dao.IPoliceDao;
import com.beordie.model.Police;
import com.beordie.utils.DBUtils;

import java.awt.image.DataBuffer;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname PoliceDaoImpl
 * @Description TODO
 * @Date 2021/7/13 22:35
 * @Created 30500
 */
public class PoliceDaoImpl extends DBUtils implements IPoliceDao {
    @Override
    public List<Police> getAll() {
        List<Police> list = new ArrayList<>();
        try {
            resultSet = super.query(SQL_SELECT_ALL, null);
            while (resultSet.next()) {
                Police police = new Police();
                police.setId(resultSet.getInt("id"));
                police.setNumberPolice(resultSet.getString("numberpolice"));
                police.setName(resultSet.getString("name"));
                police.setNumber(resultSet.getString("number"));
                police.setSex(resultSet.getInt("sex"));
                police.setAddress(resultSet.getString("address"));
                police.setPhone(resultSet.getString("phone"));
                list.add(police);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            super.closeAll();
        }
        return list;
    }

    @Override
    public List<Police> getAllByLimit(boolean isLimit, int start, int size) {
        if (!isLimit)
            return getAll();
        List<Integer> params = new ArrayList<>();
        params.add(start);
        params.add(size);
        List<Police> list = new ArrayList<>();
        try {
            resultSet = super.query(SQL_SELECT_ALL_BY_LIMIT, params);
            while (resultSet.next()) {
                Police police = new Police();
                police.setId(resultSet.getInt("id"));
                police.setNumberPolice(resultSet.getString("numberpolice"));
                police.setName(resultSet.getString("name"));
                police.setNumber(resultSet.getString("number"));
                police.setSex(resultSet.getInt("sex"));
                police.setAddress(resultSet.getString("address"));
                police.setPhone(resultSet.getString("phone"));
                list.add(police);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            super.closeAll();
        }
        return list;
    }

    @Override
    public int insert(Police police) {
        List<Object> params = new ArrayList<>();
        params.add(police.getNumberPolice());
        params.add(police.getName());
        params.add(police.getNumber());
        params.add(police.getSex());
        params.add(police.getAddress());
        params.add(police.getPhone());

        int result = 0;
        try {
            result = super.update(SQL_INSERT_ONE, params);
        } catch (Exception e) {
            e.printStackTrace();
            result = -1;
        } finally {
            super.closeAll();
        }
        return result;
    }

    @Override
    public int deleteById(int id) {
        List<Integer> params = new ArrayList<>();
        params.add(id);

        int result = 0;
        try {
            result = super.update(SQL_DELETE_BY_ID, params);
        } catch (Exception e) {
            e.printStackTrace();
            result = -1;
        } finally {
            super.closeAll();
        }
        return result;
    }

    @Override
    public int updateById(Police police, int id) {
        List<Object> params = new ArrayList<>();
        params.add(police.getNumberPolice());
        params.add(police.getName());
        params.add(police.getNumber());
        params.add(police.getSex());
        params.add(police.getAddress());
        params.add(police.getPhone());
        params.add(id);

        int result = 0;
        try {
            result = super.update(SQL_UPDATE_BY_ID, params);
        } catch (Exception e) {
            e.printStackTrace();
            result = -1;
        } finally {
            super.closeAll();
        }
        return result;
    }

    @Override
    public Police findByNumName(String number) {
        List<String> params = new ArrayList<>();
        params.add(number);
        Police police = null;

        try {
            resultSet = super.query(SQL_SELECT_BY_NUMBER, params);
            if (resultSet.next()) {
                police = new Police();
                police.setId(resultSet.getInt("id"));
                police.setNumberPolice(resultSet.getString("numberpolice"));
                police.setName(resultSet.getString("name"));
                police.setNumber(resultSet.getString("number"));
                police.setSex(resultSet.getInt("sex"));
                police.setAddress(resultSet.getString("address"));
                police.setPhone(resultSet.getString("phone"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            super.closeAll();
        }
        return police;
    }

    @Override
    public int getCount() {
        int result = 0;

        try {
            resultSet = super.query(SQL_SELECT_COUNT, null);
            if (resultSet.next())
                result = resultSet.getInt(1);
        } catch (Exception e) {
            e.printStackTrace();
            result = -1;
        } finally {
            super.closeAll();
        }
        return result;
    }
}
